package com.mccken.delay;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2024-03-19 17:20
 **/
@AllArgsConstructor
@Builder
@Data
public class MyDelayTask implements Delayed {

	// 调度的时间戳
	private Long delayMillis;

	private long start = System.currentTimeMillis();
	
	public MyDelayTask(Long delayMillis) {
		this.delayMillis = delayMillis;
	}
	
	@Override
	public long getDelay(TimeUnit unit) {
		long convert = unit.convert(delayMillis + start - System.currentTimeMillis(), TimeUnit.MICROSECONDS);
		return convert;
	}

	@Override
	public int compareTo(Delayed o) {
		MyDelayTask s = (MyDelayTask) o;
		return (int) (this.delayMillis - s.delayMillis);
	}
}
